﻿
using Tszy.Unlimited.Data.AdoNet.Sql.Components.Db.Mysql;
using Tszy.Unlimited.Data.AdoNet.Sql.Providers;

namespace Tszy.Unlimited.Data.AdoNet.Sql.Components
{
    /// <summary>
    /// SQL 数据查询器工厂。
    /// </summary>
    public interface IExecuteQueryFactory
    {
        /// <summary>
        /// 创建 SQL 数据查询器。
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="query"></param>
        /// <returns></returns>
        IExecuteQuery<TResult> Create<TSource, TResult>(IQuery<TSource> query) where TSource : new();
    }

    /// <summary>
    ///
    /// </summary>
    public class DefaultExecuteQueryFactory : IExecuteQueryFactory
    {
        private readonly IDataProvider _dataProvider;

        /// <summary>
        /// 是否显示 SQL。
        /// </summary>
        private bool _showSql = false;

        /// <summary>
        /// 输出 SQL 方法。
        /// </summary>
        private Action<string> _printSql = null;

        /// <summary>
        ///
        /// </summary>
        /// <param name="dataProvider"></param>
        /// <param name="showSql"></param>
        /// <param name="printSql"></param>
        public DefaultExecuteQueryFactory(IDataProvider dataProvider, bool showSql = false, Action<string> printSql = null)
        {
            _dataProvider = dataProvider;
            _showSql = showSql;
            _printSql = printSql;
        }

        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="query"></param>
        /// <returns></returns>
        public IExecuteQuery<TResult> Create<TSource, TResult>(IQuery<TSource> query) where TSource : new()
        {
            return new ExecuteQuery<TSource, TResult>(query, _dataProvider, _showSql, _printSql);
        }
    }
}